﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;

namespace inc
{
    namespace BaseClass
    {
        /// <summary>
        /// clsobjdotnet对象类
        /// 版权声明：本代码库采用LGPL协议发布，版权所有：流仙（Appledotnet@hotmail.com,iapple.net@gmail.com）
        /// 欢迎任何人出于合法的目的在LGPL协议许可的范围内使用和修改。
        /// </summary>
        public partial class CLSHashtable : Hashtable
        {
            //常量定义
            static readonly string STATIC_VERSION_DEFAULT = "0.5";
            static readonly string STATIC_VERSION = "0.5.0.20100505";
            static readonly string STA_HEAD_DATETIME = @"\DT";
            static readonly string STA_HEAD_BOOL = @"\BOOL";
            static readonly string STA_HEAD_STRING = @"\STR";
            static readonly string STA_HEAD_BYTE = @"\BYTE";
            bool outbysort = false;
            /// <summary>
            /// Keys是否排序后输出
            /// </summary>
            public bool OutBySort
            {
                get { return outbysort; }
                set { outbysort = value; }
            }
            //
            string version = "0.5";//默认版本
            public string Version
            {
                get
                {
                    return version;
                }
                set { version = value; }
            }
            string m_versionold = "";
            bool m_versionupdate = false;
            /// <summary>
            /// 使用新版本(用于读入旧版本的xml后输出新版本)
            /// </summary>
            public bool VersionUpdate
            {
                get
                {
                    return m_versionupdate;
                }
                set
                {
                    m_versionupdate = value;
                    m_keyrule = 0;
                    if (value)
                    {
                        if (m_versionold == "")
                            m_versionold = Version;
                        Version = STATIC_VERSION;
                        m_versiondecimal = Helper_GetVersionDecimal(Version);
                    }
                    else
                    {
                        if (m_versionold != "")
                        {
                            Version = m_versionold;
                            m_versiondecimal = Helper_GetVersionDecimal(Version);
                        }
                    }
                }
            }
            decimal m_versiondecimal = decimal.Zero;
            /// <summary>
            /// 数字版本号
            /// </summary>
            public decimal VersionDecimal
            {
                get
                {
                    if (m_versiondecimal == decimal.Zero)
                    {
                        m_versiondecimal = Helper_GetVersionDecimal(Version);
                    }
                    return m_versiondecimal;
                }
            }

            int m_keyrule = 0;
            /// <summary>
            /// XML的节点名称采用什么样的规则(1:K_Name,2:K N="Name>)
            /// </summary>
            public int KeyRule
            {
                get
                {
                    if (m_keyrule == 0)
                    {
                        ///50020100114M版本默认开始使用2号规则
                        m_keyrule = this.VersionDecimal >= 50020100114M ? 2 : 1;
                    }
                    return m_keyrule;
                }
                set { m_keyrule = value; }
            }
            //
            Hashtable m_ht_data;
            public Hashtable HT_Data
            {
                get
                {
                    return m_ht_data;
                }
            }
            string m_filename;
            /// <summary>
            /// 如果是ParseByFile,则会记录文件名
            /// </summary>
            public string FileName
            {
                get { return m_filename; }
                set { m_filename = value; }
            }
            CLSVData m_parent;
            /// <summary>
            /// 父对象
            /// 当一个CLSHashtable以包在CLSVData内的形式出现，其父对象就是一个CLSVData
            /// </summary>
            public CLSVData Parent
            {
                get
                {
                    return m_parent;
                }
                set
                {
                    m_parent = value;
                }
            }
            public CLSHashtable(Hashtable ht_data)
            {
                this.m_ht_data = new Hashtable();
                foreach (string key in ht_data.Keys)
                    this.Add(key, ht_data[key]);
            }
            public CLSHashtable()
                : this(new Hashtable())
            {
                this.Version = STATIC_VERSION;
            }
            public CLSHashtable(params object[] args)
                : this()
            {
                if (args == null || args.Length % 2 != 0)
                    throw new Exception("initialize failed 1");
                //Hashtable ht_data = new Hashtable();
                for (int i = 0; i < args.Length; i += 2)
                {
                    if (args[i] is string == false || string.IsNullOrEmpty((string)args[i]))
                        throw new Exception("initialize failed 2");
                    string key = (string)args[i];
                    if (this.m_ht_data.ContainsKey(key))
                        throw new Exception("initialize failed 3");
                    object value = args[i + 1];
                    this.Add(key, new CLSVData(this, value));
                }
                //this.m_ht_data = ht_data;
            }
            public static implicit operator CLSHashtable(object[] value)
            {
                return new CLSHashtable(value);
            }
            /// <summary>
            /// 用XML字符串创建对象(自定义导入导出)
            /// </summary>
            /// <param name="xmldata"></param>
            /// <returns></returns>
            public static CLSHashtable Parse(string xmldata)
            {
                CLSHashtable cht = new CLSHashtable();
                System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
                XmlDoc.LoadXml(xmldata);
                XmlAttribute attr = XmlDoc.DocumentElement.Attributes["version"];
                //if (attr != null) cht.Version = attr.Value;
                cht.Version = attr != null ? attr.Value : STATIC_VERSION_DEFAULT;
                //foreach (System.Xml.XmlNode Node in XmlDoc.ChildNodes[1])
                //{
                //    Func_ParseNode(cht, Node);
                //}
                Func_ParseNode(cht, XmlDoc.ChildNodes[1].ChildNodes, cht.HT_Data);
                return cht;
            }
            /// <summary>
            /// 用XML字符串创建对象(Apple NS风格)
            /// </summary>
            /// <param name="xmldata"></param>
            /// <returns></returns>
            public static CLSHashtable ParseNSStyle(string xmldata)
            {
                CLSHashtable cht = new CLSHashtable();
                System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
                XmlDoc.LoadXml(xmldata);
                //无需版本
                //XmlAttribute attr = XmlDoc.DocumentElement.Attributes["version"];
                //if (attr != null) cht.Version = attr.Value;
                XmlNode node = XmlDoc.ChildNodes[2].ChildNodes[0];
                //Func_NS_ParseNode(cht, XmlDoc.ChildNodes[1].ChildNodes, cht.HT_Data);
                Func_NS_ParseNode(cht, true, node, null);
                return cht;
            }
            /// <summary>
            /// 从文件创建对象
            /// </summary>
            /// <param name="filename"></param>
            /// <returns></returns>
            public static CLSHashtable ParseByFile(string filename)
            {
                string strall = File.ReadAllText(filename, Encoding.UTF8);
                CLSHashtable cht = inc.BaseClass.CLSHashtable.Parse(strall);
                cht.FileName = filename;
                return cht;
            }
            public static CLSHashtable ParseByNSFile(string filename)
            {
                string strall = File.ReadAllText(filename, Encoding.UTF8);
                CLSHashtable cht = CLSHashtable.ParseNSStyle(strall);
                cht.FileName = filename;
                return cht;
            }
            /// <summary>
            /// 解析一个节点(该方法是否应该做成一个非静态方法,还在考虑)
            /// </summary>
            /// <param name="cht">本质上这个对象只是为了检查版本相关的内容</param>
            /// <param name="Nodes">当前节点</param>
            /// <param name="opt">遍历到此的操作者对象</param>
            static void Func_ParseNode(CLSHashtable cht, System.Xml.XmlNodeList Nodes, object opt)
            {
                foreach (System.Xml.XmlNode Node in Nodes)
                {
                    //XmlAttribute attrtype = Node.Attributes["type"];
                    string attrtype = Node.Attributes == null || Node.Attributes["type"] == null ? "" : Node.Attributes["type"].Value;
                    string NodeName = null;
                    if (cht.KeyRule == 2)
                    {
                        //XmlAttribute attrN = Node.Attributes["N"];
                        string attrN = Node.Attributes == null || Node.Attributes["N"] == null ? "" : Node.Attributes["N"].Value;
                        if (attrN == "" && Helper_WishName(Node) && opt is ArrayList == false)//Item是ArrayList的对象集合,不需要名称,如果是添加到ArrayList里,也不要名字
                            continue;//这个节点出错了,没有名字
                        if (Helper_WishName(Node))
                            NodeName = attrN;
                    }
                    else if (cht.KeyRule == 1)
                        NodeName = Node.Name.StartsWith("K_") ? Node.Name.Remove(0, 2) : Node.Name;
                    else
                        throw new Exception("Key Rule Error");
                    if (attrtype != "")
                    {
                        if (attrtype == "al")
                        {
                            CLSArrayList al = new CLSArrayList();
                            Func_ParseNode(cht, Node.ChildNodes, al);
                            if (opt is CLSHashtable)
                                ((CLSHashtable)opt).Add(NodeName, al);
                            else if (opt is Hashtable)
                                ((Hashtable)opt).Add(NodeName, al);
                            else if (opt is ArrayList)
                                ((ArrayList)opt).Add(al);
                        }
                        else if (attrtype == "ht")
                        {
                            CLSHashtable ht = new CLSHashtable();
                            Func_ParseNode(cht, Node.ChildNodes, ht);
                            if (opt is CLSHashtable)
                                ((CLSHashtable)opt).Add(NodeName, ht);
                            else if (opt is Hashtable)
                                ((Hashtable)opt).Add(NodeName, ht);
                            else if (opt is ArrayList)
                                ((ArrayList)opt).Add(ht);
                        }
                    }
                    else
                    {
                        if (opt is ArrayList)//处理ArrayList的下级节点,2010-04-18之前,ArrayList只支持非集合类型,即不能支持Hashtable和ArrayList(那样是无限嵌套)
                        {
                            ((ArrayList)opt).Add(Node.InnerText);
                            //是否集合类型(节点下有节点,即集合类型
                            /*bool iscol = Node.HasChildNodes && Node.FirstChild.HasChildNodes;
                            if (iscol == false)
                                ((ArrayList)opt).Add(Node.InnerText);
                            else
                            {
                                //Func_ParseNode(cht, Node.ChildNodes, opt);
                            }*/
                            //2010-04-18修改逻辑,支持集合类型
                        }
                        else if (opt is CLSHashtable)
                            ((CLSHashtable)opt).Add(NodeName, Node.InnerText, true);
                        else if (opt is Hashtable)
                            ((Hashtable)opt).Add(NodeName, Node.InnerText);
                    }
                }
                return;
            }

            static void Func_NS_ParseNode(CLSHashtable cht, bool isroot, System.Xml.XmlNode node, CLSVData clsvopt)
            {
                string nodename = node.Name;
                if (Helper_NS_IsParent(node))//父节点一种处理方式
                {
                    switch (nodename)
                    {
                        //能当父节点的应该只有dict和array
                        case "dict":
                            if (node.ChildNodes.Count == 0 || node.ChildNodes.Count % 2 != 0) break;
                            for (int i = 0; i < node.ChildNodes.Count; i += 2)
                            {
                                System.Xml.XmlNode Node1 = node.ChildNodes[i];
                                System.Xml.XmlNode Node2 = node.ChildNodes[i + 1];
                                if (Node1.Name != "key")//理论上应该都是key
                                    continue;
                                bool isparent = Helper_NS_IsParent(Node2);
                                if (isparent)
                                {
                                    CLSVData clsvdobj = Helper_NS_GetParentObj(cht, Node2);
                                    if (isroot)
                                        cht[Node1.InnerText] = clsvdobj;
                                    else
                                        clsvopt[Node1.InnerText] = clsvdobj;
                                    Func_NS_ParseNode(cht, false, Node2, clsvdobj);
                                }
                                else
                                {
                                    CLSVData clsvdobj = Helper_NS_GetChildObj(cht, Node2);
                                    if (isroot)
                                        cht[Node1.InnerText] = clsvdobj;
                                    else
                                        clsvopt[Node1.InnerText] = clsvdobj;
                                }
                            }
                            break;
                        case "array":
                            //array暂时不考虑根节点的问题,目前只支持dict作为根节点(2010-04-18)
                            if (isroot)
                            {
                                //允许根节点为array,2010-04-29
                                cht["array"] = new CLSArrayList();
                                clsvopt = cht["array"];
                            }
                            for (int i = 0; i < node.ChildNodes.Count; i++)
                            {
                                System.Xml.XmlNode Node = node.ChildNodes[i];
                                bool isparent = Helper_NS_IsParent(Node);
                                if (isparent)
                                {
                                    CLSVData clsvdobj = Helper_NS_GetParentObj(cht, Node);
                                    clsvopt.ValueCLSArrayList.Add(clsvdobj.ValueRaw);
                                    Func_NS_ParseNode(cht, false, Node, clsvdobj);
                                }
                                else
                                {
                                    CLSVData clsvdobj = Helper_NS_GetChildObj(cht, Node);
                                    clsvopt.ValueCLSArrayList.Add(clsvdobj.ValueRaw);
                                }
                            }
                            break;
                        default: break;
                    }
                }
                else
                {
                }
                return;
            }
            /// <summary>
            /// 等同  new CLSHashtable(ht_data);
            /// </summary>
            /// <param name="ht_data"></param>
            /// <returns></returns>
            public static CLSHashtable Create(Hashtable ht_data)
            {
                CLSHashtable cht = new CLSHashtable(ht_data);
                return cht;
            }
            /// <summary>
            /// 接受的内容类型(有需求才扩展)
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            static bool Allow_H2A(object obj)
            {
                return obj is int || obj is string || obj is DateTime || obj is decimal || obj is Int16 || obj is Int64 || obj is bool;
            }
            /// <summary>
            /// MemoryStream到字符串
            /// </summary>
            /// <param name="ms"></param>
            /// <returns></returns>
            static string Convert_String(MemoryStream ms)
            {
                return Convert_String(ms, Encoding.UTF8);
            }
            static string Convert_String(MemoryStream ms, Encoding code)
            {
                ms.Position = 0;
                StreamReader sr = new StreamReader(ms, code);
                string rc = sr.ReadToEnd();
                sr.Close();
                return rc;
            }
            /// <summary>
            /// 把一个节点的对象转换为字符串用于存储
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public string Convert_H2X(object obj)
            {
                if (obj == null || obj is DBNull) return "";
                if (obj is DateTime)
                {
                    return STA_HEAD_DATETIME + ((DateTime)obj).Ticks.ToString();
                }
                else if (obj is bool)
                {
                    return STA_HEAD_BOOL + ((bool)obj).ToString();
                }
                else if (obj is string)
                {
                    return STA_HEAD_STRING + obj.ToString().Replace(@"\", @"\\");//把一个斜杠转成两个
                }
                else if (obj is byte[])
                {
                    return STA_HEAD_BYTE + Convert.ToBase64String((byte[])obj);
                }
                else
                    return obj.ToString();
            }
            /// <summary>
            /// 把文本解析成CLSVData友好的对象
            /// </summary>
            /// <param name="objstr"></param>
            /// <returns></returns>
            public object Convert_X2H(string objstr)
            {
                object value = null;
                if (objstr.StartsWith(@"\") && objstr.Length >= 2 && objstr[1] != '\\')//首字符是\且第二个字符不是\，则属于特殊定义
                {
                    value = Convert_X2H_T(objstr);
                }
                else
                {
                    try
                    {
                        value = decimal.Parse(objstr);
                    }
                    catch
                    {
                        value = objstr.Replace(@"\\", @"\");//把两个斜杠转成一个
                    }
                }
                return value;
            }
            public void Convert_X2H_ForCollection(object value)
            {
                if (value is Hashtable)
                {
                    Hashtable ht_value = (Hashtable)value;
                    string[] keys = new string[ht_value.Count];
                    ht_value.Keys.CopyTo(keys, 0);
                    foreach (string key in keys)
                    {
                        object objhtv = ht_value[key];
                        if (objhtv is string)
                        {
                            object clsvalue = Convert_X2H((string)objhtv);
                            CLSVData clsvdata = new CLSVData(this, clsvalue);
                            ht_value[key] = clsvdata;
                        }
                        else if (Helper_IsParent(objhtv))
                            Convert_X2H_ForCollection(objhtv);
                    }
                }
                else if (value is ArrayList)
                {
                    ArrayList al_value = (ArrayList)value;
                    for (int i = 0; i < al_value.Count; i++)
                    {
                        object objhtv = al_value[i];
                        if (objhtv is string)
                        {
                            object clsvalue = Convert_X2H((string)objhtv);
                            CLSVData clsvdata = new CLSVData(this, clsvalue);
                            al_value[i] = clsvdata;
                        }
                        else if (Helper_IsParent(objhtv))
                            Convert_X2H_ForCollection(objhtv);
                    }
                }
            }
            /// <summary>
            /// 把文本解析成CLSVData友好的对象(针对预定义的一些特殊类型)
            /// </summary>
            /// <param name="objstr"></param>
            /// <returns></returns>
            public object Convert_X2H_T(string objstr)
            {
                object value = null;
                if (objstr.StartsWith(STA_HEAD_DATETIME))
                {
                    long ticks = long.Parse(objstr.Remove(0, STA_HEAD_DATETIME.Length));
                    value = new DateTime(ticks);
                }
                else if (objstr.StartsWith(STA_HEAD_BOOL))
                {
                    value = bool.Parse(objstr.Remove(0, STA_HEAD_BOOL.Length));
                }
                else if (objstr.StartsWith(STA_HEAD_STRING))
                {
                    value = objstr.Remove(0, STA_HEAD_STRING.Length).Replace(@"\\", @"\");
                }
                else if (objstr.StartsWith(STA_HEAD_BYTE))
                {
                    value = Convert.FromBase64String(objstr.Remove(0, STA_HEAD_BYTE.Length));
                }
                else
                    value = objstr;
                return value;
            }
            /// <summary>
            /// 把Hashtable转换为ArrayList(把key/value按顺序添加进去)
            /// ***该方法未使用
            /// </summary>
            /// <param name="ht"></param>
            /// <returns></returns>
            static ArrayList Convert_H2A(CLSHashtable ht)
            {
                ArrayList AL_Data = new ArrayList();
                foreach (object key in ht.Keys)
                {
                    object obj = ht[key];
                    if (Allow_H2A(obj) == false)
                        continue;
                    AL_Data.Add(key);
                    AL_Data.Add(obj);
                }
                return AL_Data;
            }
            /// <summary>
            /// 把Hashtable转换为一个XML字符串
            /// </summary>
            /// <param name="ht"></param>
            /// <returns></returns>
            //public string Convert_H2X( Hashtable ht)
            //{
            //    MemoryStream ms = new MemoryStream();
            //    XmlTextWriter WXF = new XmlTextWriter(ms, Encoding.UTF8);
            //    WXF.Formatting = Formatting.Indented;
            //    WXF.WriteStartDocument();
            //    WXF.WriteStartElement("CLSDATA");
            //    WXF.WriteAttributeString("version", this.Version);
            //    Helper_Convert_H2X_Hashtable(WXF, ht);
            //    WXF.WriteEndElement();
            //    WXF.WriteEndDocument();
            //    WXF.Flush();
            //    string rc = Convert_String(ms, Encoding.UTF8);
            //    WXF.Close();
            //    return rc;
            //}
            /// <summary>
            /// 检查其内所有字符是否均为数字
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            static bool Helper_Check_Number(string str)
            {
                foreach (char c in str)
                {
                    if (c < '0' || c > '9')
                        return false;
                }
                return true;
            }
            /// <summary>
            /// 转换对象并写到一个XmlTextWriter,这个方法用于处理Hashtable容器
            /// </summary>
            /// <param name="WXF"></param>
            /// <param name="htb"></param>
            void Convert_H2X_Hashtable(XmlTextWriter WXF, Hashtable htb)
            {
                //传递进的Hashtable htb是兼容处理CLSHashtable的,因为CLSHashtable继承自Hashtable,所以CLSHashtable也是一个Hashtable
                CLSHashtable ht = htb is CLSHashtable ? (CLSHashtable)htb : new CLSHashtable(htb);
                ht.OutBySort = true;
                string[] keys = ht.KeyArray;
                foreach (object key in keys)
                {
                    if (this.KeyRule == 1)
                    {
                        WXF.WriteStartElement("K_" + key.ToString());
                    }
                    else if (this.KeyRule == 2)
                    {
                        WXF.WriteStartElement("K");
                        WXF.WriteAttributeString("N", key.ToString());
                    }
                    else
                        throw new Exception("Key Rule Error");
                    object value = ht[key];
                    if (value is CLSHashtable)
                        WXF.WriteAttributeString("type", "ht");
                    if (value is CLSVData && ((CLSVData)value).Value is CLSHashtable)
                        WXF.WriteAttributeString("type", "ht");
                    if (value is CLSVData && ((CLSVData)value).Value is ArrayList)
                        WXF.WriteAttributeString("type", "al");
                    if (value is ArrayList)
                        WXF.WriteAttributeString("type", "al");
                    Convert_H2X_Obj(WXF, value);
                    WXF.WriteEndElement();
                }
            }
            void Convert_NS_H2X_Hashtable(XmlTextWriter WXF, Hashtable htb)
            {
                CLSHashtable ht = htb is CLSHashtable ? (CLSHashtable)htb : new CLSHashtable(htb);
                string[] keys = ht.KeyArray;
                WXF.WriteStartElement("dict");
                foreach (string key in keys)
                {
                    WXF.WriteStartElement("key");
                    WXF.WriteString(key);
                    WXF.WriteEndElement();
                    object htvalue = ht[key];
                    Convert_NS_H2X_Value(WXF, htvalue);
                }
                WXF.WriteEndElement();
            }
            void Convert_NS_H2X_ArrayList(XmlTextWriter WXF, ArrayList al)
            {
                WXF.WriteStartElement("array");
                for (int i = 0; i < al.Count; i++)
                {
                    Convert_NS_H2X_Value(WXF, al[i]);
                }
                WXF.WriteEndElement();
            }
            void Convert_NS_H2X_Value(XmlTextWriter WXF, object htvalue)
            {
                if (htvalue is CLSVData)
                    htvalue = ((CLSVData)htvalue).ValueRaw;
                string vtype = "";
                bool isparent = Helper_NS_IsParent(htvalue, ref vtype);
                if (isparent)
                {
                    if (vtype == "dict")
                        Convert_NS_H2X_Hashtable(WXF, (Hashtable)htvalue);
                    else if (vtype == "array")
                        Convert_NS_H2X_ArrayList(WXF, (ArrayList)htvalue);
                }
                else
                {
                    vtype = Helper_NS_GetChildType(htvalue);
                    string valuestr = Helper_NS_GetChildValueStr(vtype, htvalue);
                    WXF.WriteStartElement(vtype);
                    WXF.WriteString(valuestr);
                    WXF.WriteEndElement();
                }
            }
            /// <summary>
            /// 转换对象并写到一个XmlTextWriter,这个方法用于处理ArrayList容器
            /// </summary>
            /// <param name="WXF"></param>
            /// <param name="al"></param>
            void Convert_H2X_ArrayList(XmlTextWriter WXF, ArrayList al)
            {
                foreach (object value in al)
                {
                    string type = "";
                    bool isparent = Helper_IsParent(value, ref type);
                    WXF.WriteStartElement("Item");
                    if (isparent)
                    {
                        //WXF.WriteStartElement("K");
                        WXF.WriteAttributeString("type", type);
                    }
                    Convert_H2X_Obj(WXF, value);
                    //if (isparent)
                    //    WXF.WriteEndElement();
                    WXF.WriteEndElement();
                }
            }
            /// <summary>
            /// 转换对象并写到一个XmlTextWriter,这个方法用于处理CLSVData容器
            /// </summary>
            /// <param name="WXF"></param>
            /// <param name="clsvdata"></param>
            void Convert_H2X_CLSVData(XmlTextWriter WXF, CLSVData clsvdata)
            {
                if (clsvdata.HasAttributes)//对属性作处理
                {
                    string[] keys = clsvdata.Attributes.Keys;
                    foreach (string key in keys)//属性只处理一层
                    {
                        if (clsvdata.Attributes[key].Value is Hashtable && ((Hashtable)clsvdata.Attributes[key].Value).Count == 0)
                            continue;
                        WXF.WriteAttributeString(key, Convert_H2X(clsvdata.Attributes[key].Value));
                    }
                }
                Convert_H2X_Obj(WXF, clsvdata.Value);
            }
            /// <summary>
            /// 转换对象并写到一个XmlTextWriter,这个方法用于处理任意对象——实际是递归调用，比如Hashtable里有ArrayList
            /// </summary>
            /// <param name="WXF"></param>
            /// <param name="value"></param>
            void Convert_H2X_Obj(XmlTextWriter WXF, object value)
            {
                if (value is CLSHashtable)
                {
                    Convert_H2X_Hashtable(WXF, (CLSHashtable)value);
                }
                else if (value is ArrayList)
                {
                    Convert_H2X_ArrayList(WXF, (ArrayList)value);
                }
                else if (value is CLSVData)
                {
                    Convert_H2X_CLSVData(WXF, (CLSVData)value);
                }
                else
                    WXF.WriteString(Convert_H2X(value));
            }
            /// <summary>
            /// 把文本版本号转换成数字版本号
            /// </summary>
            /// <param name="version">文本版本号</param>
            /// <returns></returns>
            static decimal Helper_GetVersionDecimal(string version)
            {
                string[] vs = version.Split('.');
                if (vs.Length != 4) return decimal.Zero;//必须由4部分组成
                CLSObject[] clsos = { vs[0], vs[1], vs[2], vs[3] };
                decimal rc = clsos[0].ValueDecimal * 100 * 100 * 100000000 + clsos[1].ValueDecimal * 100 * 100000000 + clsos[2].ValueDecimal * 100000000 + clsos[3].ValueDecimal;
                return rc;
            }
            /// <summary>
            /// 在K N="name"的模式里,不是所有节点都需要名字,这个方法用于检查哪些节点是不需要名字的
            /// </summary>
            /// <param name="Node"></param>
            /// <returns></returns>
            static bool Helper_WishName(XmlNode Node)
            {
                return Node.Name != "Item";
            }
            static bool Helper_IsParent(object value)
            {
                string type = "";
                return Helper_IsParent(value, ref type);
            }
            static bool Helper_IsParent(object value, ref string type)
            {
                if (value is Hashtable)
                    type = "ht";
                else if (value is ArrayList)
                    type = "al";
                return value is Hashtable || value is ArrayList;
            }
            static bool Helper_NS_IsParent(object value, ref string type)
            {
                if (value is Hashtable)
                    type = "dict";
                else if (value is ArrayList)
                    type = "array";
                return value is Hashtable || value is ArrayList;
            }
            static bool Helper_NS_IsParent(XmlNode Node)
            {
                return Helper_NS_IsParent(Node.Name);
            }
            static bool Helper_NS_IsParent(string type)
            {
                return type == "dict" || type == "array";
            }
            static CLSVData Helper_NS_GetParentObj(CLSHashtable cht, XmlNode Node)
            {
                if (Node.Name == "dict")
                    return new CLSVData(cht, new CLSHashtable());
                else if (Node.Name == "array")
                    return new CLSVData(cht, new CLSArrayList());
                else
                {
                    throw new Exception("该类别不能创建父对象");
                    return null;
                }
            }
            static string Helper_NS_GetChildType(object value)
            {
                if (value is CLSVData)
                    value = ((CLSVData)value).ValueRaw;
                if (value is byte[])
                    return "data";
                else if (value is DateTime)
                    return "date";
                else if (value is decimal)
                    return "real";
                else if (value is int)
                    return "integer";
                else if (value is string)
                    return "string";
                else if (value is bool)
                {
                    return (bool)value ? "true" : "false";
                }
                else
                    return "";
            }
            static string Helper_NS_GetChildValueStr(string type, object value)
            {
                if (value is CLSVData)
                    value = ((CLSVData)value).ValueRaw;
                if (type == "data")
                {
                    return Convert.ToBase64String((byte[])value);
                }
                else if (type == "date")
                {
                    return ((DateTime)value).ToUniversalTime().ToString("s") + "Z";
                }
                else if (type == "real")
                {
                    return ((decimal)value).ToString();
                }
                else if (type == "integer")
                {
                    return ((int)value).ToString();
                }
                else if (type == "string")
                {
                    return (string)value;
                }
                else if (type == "true")
                {
                    return "";// ((bool)value).ToString().ToLower();
                }
                else if (type == "false")
                {
                    return "";// ((bool)value).ToString().ToLower();
                }
                else
                {
                    throw new Exception("没有这个类别");
                    return null;
                }
            }
            static CLSVData Helper_NS_GetChildObj(CLSHashtable cht, XmlNode Node)
            {
                return Helper_NS_GetChildObj(cht, Node.Name, Node.InnerText);
            }
            static CLSVData Helper_NS_GetChildObj(CLSHashtable cht, string type, string text)
            {
                //string text = Node.InnerText;
                if (type == "data")
                {
                    byte[] bytes = Convert.FromBase64String(text);
                    return bytes;
                }
                else if (type == "date")
                {
                    return DateTime.Parse(text);
                }
                else if (type == "real")
                {
                    return decimal.Parse(text);
                }
                else if (type == "integer")
                {
                    return int.Parse(text);
                }
                else if (type == "string")
                {
                    return text;
                }
                else if (type == "true")
                {
                    return true;
                }
                else if (type == "false")
                {
                    return false;
                }
                else
                {
                    throw new Exception("该类别不能创建父对象");
                    return null;
                }
            }
            /// <summary>
            /// 把CLSHashtable对象(this)导出成xml形式
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                MemoryStream ms = new MemoryStream();
                XmlTextWriter WXF = new XmlTextWriter(ms, Encoding.UTF8);
                WXF.Formatting = Formatting.Indented;
                WXF.WriteStartDocument();
                WXF.WriteStartElement("CLSDATA");
                WXF.WriteAttributeString("version", this.Version);
                Convert_H2X_Hashtable(WXF, this.HT_Data);
                WXF.WriteEndElement();
                WXF.WriteEndDocument();
                WXF.Flush();
                string rc = Convert_String(ms, Encoding.UTF8);
                WXF.Close();
                return rc;
            }
            public string ToNSString()
            {
                MemoryStream ms = new MemoryStream();
                XmlTextWriter WXF = new XmlTextWriter(ms, Encoding.UTF8);
                WXF.Formatting = Formatting.Indented;
                WXF.WriteStartDocument();
                WXF.WriteDocType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
                WXF.WriteStartElement("plist");
                WXF.WriteAttributeString("version", "1.0");
                Convert_NS_H2X_Hashtable(WXF, this.HT_Data);
                WXF.WriteEndElement();
                WXF.WriteEndDocument();
                WXF.Flush();
                string rc = Convert_String(ms, Encoding.UTF8);
                WXF.Close();
                return rc;
            }
            public string ToNSArrayString()
            {
                //仅处理一种特例:只有一个key:array,且值为CLSArrayList
                CLSVData v = this["array"];
                if (v.ValueRaw is CLSArrayList == false)
                    throw new Exception("不能被转换为NSArray String");
                CLSArrayList CLA_Data = (CLSArrayList)v.ValueRaw;
                MemoryStream ms = new MemoryStream();
                XmlTextWriter WXF = new XmlTextWriter(ms, Encoding.UTF8);
                WXF.Formatting = Formatting.Indented;
                WXF.WriteStartDocument();
                WXF.WriteDocType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
                WXF.WriteStartElement("plist");
                WXF.WriteAttributeString("version", "1.0");
                //Convert_NS_H2X_Hashtable(WXF, CLA_Data);
                Convert_NS_H2X_ArrayList(WXF, CLA_Data);
                WXF.WriteEndElement();
                WXF.WriteEndDocument();
                WXF.Flush();
                string rc = Convert_String(ms, Encoding.UTF8);
                WXF.Close();
                return rc;
            }
            /// <summary>
            /// 保存到文件（由文件解析出来的对象可以不提供文件名）
            /// </summary>
            public void ToFile()
            {
                this.ToFile(this.FileName);//由ParseByFile创建的对象，可以不用传入保存文件名
            }
            /// <summary>
            /// 保存到文件
            /// </summary>
            /// <param name="filename"></param>
            public void ToFile(string filename)
            {
                if (filename == null || filename == "")
                    throw new Exception("保存路径不存在，请仔细检查");
                using (StreamWriter sw = File.CreateText(filename))
                {
                    sw.Write(this.ToString());
                }
            }
            public void ToNSFile()
            {
                this.ToNSFile(this.FileName);
            }
            public void ToNSFile(string filename)
            {
                if (filename == null || filename == "")
                    throw new Exception("保存路径不存在，请仔细检查");
                using (StreamWriter sw = File.CreateText(filename))
                {
                    sw.Write(this.ToNSString());
                }
            }
            public override void Add(object key, object value)
            {
                Add(key, value, false);
            }
            public void Add(object key, object value, bool parse)
            {
                if (key is string == false)
                    throw new Exception("添加进列表的KEY必须为字符串");
                m_keychange = true;
                if (value is CLSVData)
                    this.m_ht_data.Add(key, value);
                else if (parse && value is string)
                {
                    //用于读取数据时的parse操作
                    this.m_ht_data.Add(key, (string)value);
                    //object vc = Convert_X2H_T((string)value);
                    //this.m_ht_data.Add(key, new CLSVData(this, value));
                    //this.m_ht_data.Add(key, vc);
                }
                else if (value is ArrayList && (value is CLSArrayList == false))
                {
                    ArrayList AL = (ArrayList)value;
                    CLSArrayList CLSAV = new CLSArrayList();
                    foreach (object obj in AL)
                        CLSAV.Add(obj);
                    this.m_ht_data.Add(key, CLSAV);
                }
                else
                    this.m_ht_data.Add(key, new CLSVData(this, value));
            }
            public override void Remove(object key)
            {
                m_keychange = true;
                this.m_ht_data.Remove(key);
            }
            public override void Clear()
            {
                m_keychange = true;
                this.m_ht_data.Clear();
            }
            public override int Count
            {
                get
                {
                    return this.m_ht_data.Count;
                }
            }
            public override ICollection Keys
            {
                get
                {
                    return this.m_ht_data.Keys;
                }
            }
            bool m_keychange = true;
            string[] m_keyarray = new string[0];
            /// <summary>
            /// Key的列表
            /// </summary>
            public string[] KeyArray
            {
                get
                {
                    if (m_keychange)
                    {
                        Hashtable ht_value = (Hashtable)this.HT_Data;
                        string[] keys = new string[ht_value.Count];
                        int index = 0;
                        foreach (object key in ht_value.Keys)
                        {
                            keys[index] = key.ToString();
                            index++;
                        }
                        if (this.OutBySort) Array.Sort(keys);
                        m_keyarray = keys;
                        m_keychange = false;
                    }
                    return m_keyarray;
                }
            }
            public override bool ContainsKey(object key)
            {
                return this.m_ht_data.ContainsKey(key);
            }
            /// <summary>
            /// 索引器来实现方便的访问
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public new CLSVData this[object key]
            {
                get
                {
                    CLSVData rc = null;
                    object obj = this.m_ht_data[key];
                    if (obj == null)
                    {
                        CLSVData clsvdata = new CLSVData(this, new CLSHashtable());
                        this.Add(key, clsvdata);
                        rc = clsvdata;
                    }
                    else if (obj is CLSVData)
                        rc = (CLSVData)obj;
                    else if (obj is CLSHashtable)
                        rc = new CLSVData(obj);
                    else if (obj is ArrayList)
                        rc = new CLSVData(this, obj, true);
                    else if (obj is string)//文本解析到对象事实上是在这里做的，即不读就是字符串，读的时候就解析
                    {
                        string objstr = (string)obj;
                        object value = Convert_X2H(objstr);
                        CLSVData clsvdata = new CLSVData(this, value);
                        this[key] = clsvdata;
                        rc = clsvdata;
                    }
                    else
                    {
                        CLSVData clsvdata = new CLSVData(this, new CLSHashtable());
                        this[key] = clsvdata;
                        //this.Add(key, clsvdata);
                        rc = clsvdata;
                    }
                    rc.Container = this;
                    return rc;
                }
                set
                {
                    if (value == null)
                        this.m_ht_data.Remove(key);
                    else
                    {
                        value.Container = this;
                        if (this.m_ht_data.ContainsKey(key) == false)
                        {
                            /*if (value is CLSVData)
                            {
                                CLSVData data = (CLSVData)value;
                                data.Parent = this;
                                data.Container = this.Container;
                                ht_value.Add(key, data);
                            }
                            else*/
                            m_keychange = true;
                            this.m_ht_data.Add(key, value);
                        }
                        else
                            this.m_ht_data[key] = value;
                    }
                }
            }
        }
        public partial class CLSVData
        {
            CLSHashtable m_container;
            /// <summary>
            /// 顶端容器
            /// </summary>
            public CLSHashtable Container
            {
                get
                {
                    return m_container;
                }
                set
                {
                    m_container = value;
                }
            }
            CLSVData m_parent;
            /// <summary>
            /// 父对象
            /// 因为父对象即使是CLSHashtable也会被以包在CLSVData内的形式出现，所以父对象一定且必须是CLSVData
            /// </summary>
            public CLSVData Parent
            {
                get
                {
                    return m_parent;
                }
                set
                {
                    m_parent = value;
                }
            }
            object m_value;
            object m_valueraw;
            /// <summary>
            /// Value表示该CLSData里实际上是什么对象
            /// </summary>
            public object Value
            {
                get
                {
                    return m_value;
                }
                set
                {
                    m_valueraw = value;
                    m_value = value;
                }
            }
            public object ValueRaw
            {
                get
                {
                    if (m_valueraw == null)
                        return this.Value;
                    return m_valueraw;
                }
                set
                {
                    m_valueraw = value;
                }
            }
            public DateTime ValueDateTime
            {
                get
                {
                    if (Value is DateTime)
                        return (DateTime)Value;
                    else
                        return new DateTime(1970, 1, 1);
                }
            }
            public bool ValueBool
            {
                get
                {
                    if (Value is bool)
                        return (bool)Value;
                    else
                        return false;
                }
            }
            public string ValueString
            {
                get
                {
                    if (Value is string)
                        return (string)Value;
                    else
                        return "";
                }
            }
            public CLSObject ValueCCLSObject
            {
                get
                {
                    if (Value is string)
                        return (CLSObject)(string)Value;
                    else
                        return (CLSObject)"";
                }
            }
            public byte[] ValueByteArray
            {
                get
                {
                    return this.Value is byte[] ? (byte[])this.Value : new byte[0];
                }
            }
            public ArrayList ValueArrayList
            {
                get
                {
                    if (ValueIsNull)//是默认值，无内容，即允许即时创建所想要的对象
                        this.Value = new ArrayList();
                    if (this.Value is ArrayList)
                        return (ArrayList)Value;
                    else
                    {
                        //无法提供默认值,只能停止操作以作提示
                        throw new Exception("该值不能提供默认");
                        return null;
                    }
                }
            }
            public CLSArrayList ValueCLSArrayList
            {
                get
                {
                    if (ValueIsNull)//是默认值，无内容，即允许即时创建所想要的对象
                        this.Value = new CLSArrayList();
                    if (this.Value is CLSArrayList)
                        return (CLSArrayList)Value;
                    else
                    {
                        //无法提供默认值,只能停止操作以作提示
                        throw new Exception("该值不能提供默认");
                        return null;
                    }
                }
            }
            public decimal ValueDecimal
            {
                get
                {
                    if (Value is decimal)
                        return (decimal)Value;
                    else
                        return decimal.Zero;
                }
                set
                {
                    if (Value is decimal)
                    {
                        Value = value;
                    }
                }
            }
            public int ValueInt
            {
                get
                {
                    return (int)ValueDecimal;
                }
                set
                {
                    ValueDecimal = value;
                }
            }
            public bool ValueIsNull
            {
                get
                {
                    return (Value is Hashtable) && ((Hashtable)Value).Count == 0;
                }
            }
            public bool ValueIsCollection
            {
                get
                {
                    return Value is Hashtable || Value is ArrayList;
                }
            }
            /// <summary>
            /// 容器当前对象个数
            /// </summary>
            public int Length
            {
                get
                {
                    if (this.Value is Hashtable)
                        return ((Hashtable)this.Value).Count;
                    else
                        return 0;
                }
            }
            /// <summary>
            /// 因为如果用Hashtable的Keys，在foreach时get索引器有很多行为会导致改写，从而导致foreach失败
            /// </summary>
            public string[] Keys
            {
                get
                {
                    if (this.Value is CLSHashtable)
                    {
                        return ((CLSHashtable)this.Value).KeyArray;
                    }
                    return new string[0];
                }
            }
            /// <summary>
            /// 以一个CLSHashtable为顶端容器创建一个CLSVData
            /// </summary>
            /// <param name="cht"></param>
            /// <param name="value"></param>
            public CLSVData(CLSHashtable cht, object value)
                : this(cht, value, true)
            {
                this.Parent = cht;
            }
            /// <summary>
            /// 以一个CLSVData为父对象创建一个CLSVData
            /// </summary>
            /// <param name="clsvd"></param>
            /// <param name="value"></param>
            public CLSVData(CLSVData clsvd, object value)
                : this(clsvd.Container, value, true)
            {
                //this.Container = clsvd.Container;
                this.Parent = clsvd;
                //this.Value = value;
            }
            /// <summary>
            /// 指定value创建一个CLSVData
            /// </summary>
            /// <param name="value"></param>
            public CLSVData(object value)
                : this(null, value, true)
            {
            }
            public CLSVData(CLSHashtable cht, object value, bool convert)
            {
                if (Helper_CCDecimal(value))
                {
                    this.Value = Convert.ToDecimal(value);
                    //被强制转换为Decimal的，保留原类型数据
                    this.ValueRaw = value;
                }
                else
                    this.Value = value;
                this.Container = cht;
                //this.Value = value;
                if (value is CLSHashtable)
                    ((CLSHashtable)value).Parent = this;
                /*if (convert && value is ArrayList)//对ArrayList作特殊处理——将其内对象作CLSVData友好的转换
                {
                    //这里作处理只应该对Parse时的数据作处理，调用者自行添加的对象原则上不应该经过这里。
                    ArrayList AL_Data = (ArrayList)value;
                    for (int i = 0; i < AL_Data.Count; i++)
                    {
                        if (AL_Data[i] is string)
                        {
                            string objstr = (string)AL_Data[i];
                            object valueal = Container.Convert_X2H(objstr);
                            AL_Data[i] = valueal;
                        }
                    }
                }*/
                if (convert && (value is ArrayList || value is Hashtable) && Container != null)
                {
                    Container.Convert_X2H_ForCollection(value);
                }
            }
            /*隐式转换方法
             * 从文本导出时并未写入类型,有几个原因
             * 1、避免导出文本过于臃肿；
             * 2、设计本方法即是出于简化编程的目的，设计者希望尽量做成弱类型，所以比如数值就全部归到decimal
             * */
            public static implicit operator CLSVData(CLSHashtable value)
            {
                return new CLSVData(value);
            }
            public static implicit operator CLSVData(decimal value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(double value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(float value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(byte value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(char value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(int value)
            {
                //int不转换成decimal处理,单独处理,以便可以用ValueRaw调用,并兼容NS风格,另外是否在存储时区分,需要再考虑
                return new CLSVData(value);
            }
            public static implicit operator CLSVData(long value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(sbyte value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(short value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(uint value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(ulong value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(ushort value)
            {
                return new CLSVData((decimal)value);
            }
            public static implicit operator CLSVData(string value)
            {
                return new CLSVData(value);
            }
            public static implicit operator CLSVData(DateTime value)
            {
                return new CLSVData(value);
            }
            public static implicit operator CLSVData(bool value)
            {
                return new CLSVData(value);
            }
            public static implicit operator CLSVData(ArrayList value)
            {
                return new CLSVData(null, (object)value, false);
            }
            public static implicit operator CLSVData(byte[] value)
            {
                return new CLSVData(null, (object)value, false);
            }
            public static implicit operator CLSVData(object[] value)
            {
                ArrayList AL_Data = new ArrayList();
                foreach (object obj in value)
                {
                    AL_Data.Add(obj);
                }
                return new CLSVData(AL_Data);
            }
            //
            //更多转换有待添加
            public CLSVData this[object key]
            {
                get
                {
                    if (this.Value is CLSHashtable)
                    {
                        CLSHashtable ht_value = (CLSHashtable)this.Value;
                        return ht_value[key];
                        /*object obj = ht_value[key];
                        if (obj == null)
                        {
                            CLSVData clsvdata = new CLSVData(this, obj);
                            ht_value.Add(key, obj);
                        }
                        else if (obj is CLSVData)
                            return (CLSVData)obj;
                        else if (obj is string)//解析文本因都会经过这里,因为刚解析出来都是文本的
                        {
                            object value = null;
                            try
                            {
                                value = decimal.Parse((string)obj);
                            }
                            catch
                            {
                                value = (string)obj;
                            }
                            CLSVData clsvdata = new CLSVData(this, value);
                            ht_value[key] = clsvdata;
                        }*/
                    }
                    return new CLSVData(this, null);
                }
                set
                {
                    if (this.Value is CLSHashtable)
                    {
                        CLSHashtable ht_value = (CLSHashtable)this.Value;
                        ht_value[key] = value;
                        if (ht_value.ContainsKey(key) == false)
                        {
                            if (value is CLSVData)
                            {
                                CLSVData data = (CLSVData)value;
                                data.Parent = this;
                                data.Container = this.Container;
                            }
                        }
                        /*if (value == null)
                            ht_value.Remove(key);
                        else if (ht_value.ContainsKey(key) == false)
                        {
                            if (value is CLSVData)
                            {
                                CLSVData data = (CLSVData)value;
                                data.Parent = this;
                                data.Container = this.Container;
                                ht_value.Add(key, data);
                            }
                            else
                                ht_value.Add(key, value);
                        }
                        else
                            ht_value[key] = value;*/
                    }
                }
            }
            public override string ToString()
            {
                if (this.Value == null)
                    return "null";
                else
                    return this.Value.ToString();
            }
            CLSVData m_attributes;
            public CLSVData Attributes
            {
                get
                {
                    if (m_attributes == null)
                    {
                        CLSHashtable cht = new CLSHashtable();
                        m_attributes = new CLSVData(cht);
                    }
                    return m_attributes;
                }
            }
            public bool HasAttributes
            {
                get
                {
                    return m_attributes != null && m_attributes.Length > 0;
                }
            }
            public static bool Helper_CCDecimal(object value)
            {
                return value is decimal || value is sbyte || value is byte || value is short || value is ushort || value is int || value is uint || value is long || value is ulong || value is char || value is float || value is double;
            }
        }
        public partial class CLSAttributeCollection
        {
        }
        /// <summary>
        /// 用来代替XmlTextWriter进行自己的XML拼装（未完成）
        /// </summary>
        public partial class CLSXmlTextWriter
        {
            StringBuilder sb = new StringBuilder();
            public void WriteStartDocument()
            {
            }
            public void WriteStartElement()
            {
            }
            public void WriteAttributeString()
            {
            }
            public void WriteEndDocument()
            {
            }
            public void WriteEndElement()
            {
            }
        }
        public partial class CLSObject
        {
            object m_value = null;
            public object Value
            {
                get { return m_value; }
                set { m_value = value; }
            }
            public CLSObject(object value)
            {
                this.Value = value;
            }
            public static implicit operator CLSObject(string value)
            {
                return new CLSObject(value);
            }
            public static implicit operator CLSObject(int value)
            {
                return new CLSObject(value);
            }
            public static implicit operator CLSObject(decimal value)
            {
                return new CLSObject(value);
            }
            public bool IsString
            {
                get
                {
                    return this.Value is string && this.Value != null;
                }
            }
            public decimal ValueDecimal
            {
                get
                {
                    if (this.Value is decimal)
                        return (decimal)this.Value;
                    else if (this.Value == null)
                        return decimal.Zero;
                    else
                    {
                        try
                        {
                            return decimal.Parse(this.Value.ToString());
                        }
                        catch
                        {
                            return decimal.Zero;
                        }
                    }
                }
            }
            public int ValueInt32
            {
                get
                {
                    return (int)this.ValueDecimal;
                }
            }
            public Int64 ValueInt64
            {
                get
                {
                    return (Int64)this.ValueDecimal;
                }
            }
            public bool ValueBool
            {
                get
                {
                    return this.ValueString.ToLower() == "true";
                }
            }
            public string ValueString
            {
                get
                {
                    if (this.Value != null)
                    {
                        if (this.Value is string)
                            return (string)this.Value;
                        else
                            return this.Value.ToString();
                    }
                    else
                        return "";
                }
            }
            public bool ValueIsNull
            {
                get
                {
                    //哪些情况将被视为null
                    if (this.Value == null)
                        return true;
                    else if (this.Value is string && (string)this.Value == "")
                        return true;
                    else
                        return false;
                }
            }
            public override string ToString()
            {
                return this.ValueString;
            }
            public static CLSObject[] ParseStringSplit(string str, string Split)
            {
                string[] strs = str.Split(Split.ToCharArray());
                CLSObject[] clsos = new CLSObject[strs.Length];
                for (int i = 0; i < strs.Length; i++)
                    clsos[i] = (CLSObject)strs[i];
                return clsos;
            }
        }
        public partial class CLSNameValue
        {
            string m_name;
            CLSVData m_value;
            public string Name
            {
                get
                {
                    return m_name;
                }
                set { m_name = value; }
            }
            public CLSVData Value
            {
                get
                {
                    return m_value;
                }
                set
                {
                    m_value = value;
                }
            }
            public CLSNameValue()
            {
            }
            public CLSNameValue(string Name, CLSVData Value)
            {
                this.Name = Name;
                this.Value = Value;
            }
        }
        public partial class CLSNameValueCollection
        {
            Hashtable m_cht_data;
            public CLSNameValueCollection()
            {
                m_cht_data = new Hashtable();
            }
            public CLSObject this[string key]
            {
                get
                {
                    if (m_cht_data.ContainsKey(key))
                        return (CLSObject)m_cht_data[key];
                    else
                        return "";
                }
                set
                {
                    m_cht_data[key] = value;
                }
            }
        }
        public partial class CLSHelper
        {
            public static bool ValueIsNull(object value)
            {
                if (value == null)
                    return true;
                else if (value is CLSHashtable && ((CLSHashtable)value).Count == 0)
                    return true;
                else if (value is CLSArrayList && ((CLSArrayList)value).Count == 0)
                    return true;
                return false;
            }
        }
        public partial class CLSArrayList:ArrayList
        {
            CLSHashtable cht = new CLSHashtable();//为符合语法而设,不作实际用途
            string m_filename = "";
            public CLSArrayList()
                : base()
            {
                cht["array"] = this;
            }
            public override int Add(object value)
            {
                CLSVData clsvd = new CLSVData(cht, value);
                return base.Add(clsvd);
            }
            public int AddDict(params object[] args)
            {
                //添加一个hashtable进来
                if (args.Length % 2 != 0)
                    return -1;
                CLSHashtable ht = new CLSHashtable();
                for (int i = 0; i < args.Length; i += 2)
                {
                    ht.Add(args[i], args[i + 1]);
                }
                return base.Add(ht);
            }
            public CLSHashtable AddNodeCLSHashtable()
            {
                CLSHashtable ht = new CLSHashtable();
                base.Add(ht);
                return ht;
            }
            /// <summary>
            /// 尝试获取一个CLSHashtable类型的元素(如不是,尽量尝试转换)
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public CLSHashtable GetValueCLSHT(int index)
            {
                object obj = this[index];
                CLSHashtable cht_line = null;
 
                if (obj is CLSVData && ((CLSVData)obj).ValueRaw is CLSHashtable)
                    cht_line = (CLSHashtable)((CLSVData)obj).ValueRaw;
                if (obj is CLSHashtable)
                    cht_line = (CLSHashtable)obj;
                return cht_line;
            }
            public override void Clear()
            {
                base.Clear();
            }
            public override int Count
            {
                get
                {
                    return base.Count;
                }
            }
            public override void RemoveAt(int index)
            {
                base.RemoveAt(index);
            }
            /*public override object this[int index]
            {
                get
                {
                    return base[index];
                }
                set
                {
                    base[index] = value;
                }
            }*/
            /*public new CLSVData this[int index]
            {
                get
                {
                    object obj = base[index];
                    if (obj is CLSVData)
                        return (CLSVData)obj;
                    else
                    {
                        CLSVData clsvd = new CLSVData(cht, obj);
                        base[index] = clsvd;
                        return clsvd;
                    }
                }
                set
                {
                    if (value is CLSVData)
                        base[index] = value;
                    else
                        base[index] = new CLSVData(cht, value);
                }
            }*/
            public override object[] ToArray()
            {
                return base.ToArray();
            }
            public string ToNSString()
            {
                return cht.ToNSArrayString();
            }
            public void ToNSFile()
            {
                ToNSFile(this.m_filename);
            }
            public void ToNSFile(string filename)
            {
                if (filename == null || filename == "")
                    throw new Exception("保存路径不存在，请仔细检查");
                using (StreamWriter sw = File.CreateText(filename))
                {
                    sw.Write(this.ToNSString());
                }
            }
            public static CLSArrayList ParseNSStyle(string xmldata)
            {
                CLSHashtable cht = CLSHashtable.ParseNSStyle(xmldata);
                CLSVData v = cht["array"];
                if (v.ValueRaw is CLSArrayList == false)
                    throw new Exception("文件格式不正确");
                CLSArrayList CLA_Data = (CLSArrayList)v.ValueRaw;
                return CLA_Data;
            }
            public static CLSArrayList ParseByNSFile(string filename)
            {
                string strall = File.ReadAllText(filename, Encoding.UTF8);
                return ParseNSStyle(strall);
            }
        }
    }
}